# -*- coding: utf-8 -*-
import pymysql

from odoo import models, fields, api


class UserInfo(models.Model):
    _name = 'etc.user.info'
    _description = 'ETC User Info'

    centerUserId = fields.Char(string='用户编号', help='同中创接口中的“用户编号”')
    userClass = fields.Selection([("1", "个人"), ("2", "单位")], string='用户类别')
    name = fields.Char(string='名称')
    idType = fields.Selection([("101", "身份证（含临时身份证）"), ("102", "护照（限外籍人士）"), ("103", "港澳居民来往内地通行证"),
                               ("104", "台湾居民来往大陆通行证"), ("105", "军官证"), ("106", "武警警察身份证"), ("113", "澳门永久/非永久居民身份证"),
                               ("114", "香港永久/非永久居民身份证")], string='证件类型')
    idNo = fields.Char(string='证件号码')
    phone = fields.Char(string='联系电话')
    address = fields.Char(string='联系地址')
    email = fields.Char()
    idPicUrl = fields.Char(string='个人/经办人照片1')
    idPicReverseUrl = fields.Char(string='个人/经办人照片2')
    unitBank = fields.Char(string='单位开户行')
    unitBankAddress = fields.Char(string='单位开户行地址')
    unitBankAccount = fields.Char(string='单位开户行账号')
    unitTaxNumber = fields.Char(string='单位纳税人识别号')
    unitImage = fields.Char(string='单位证件图片', default='')
    agentName = fields.Char(string='经办人姓名')
    agentIdType = fields.Selection([("101", "身份证")], default='101', string='经办人证件类型')
    agentIdNum = fields.Char(string='经办人证件号码')
    agentAddress = fields.Char(string='经办人地址')

    vehicle_ids = fields.One2many('etc.vehicle.info', 'userinfo_id', string='车牌号码')
    card_ids = fields.One2many('etc.card.info', 'userinfo_id', string='ETC卡号')

    # def read(self, fields=None, load='_classic_read'):
    #     # import urllib.request
    #     # post_url = "http://10.200.251.52:8000/list/1"
    #     # request_param = bytes(urllib.parse.urlencode({"usernames": "1"}), encoding='utf8')
    #     # request = urllib.request.Request(post_url, data=request_param, method='GET')
    #     # a = urllib.request.urlopen(request)
    #     # print(a.read().decode())
    #
    #     import pymysql
    #     conn = pymysql.connect(host='112.126.88.195',port=3306,user='zshr',password='zshr',database='etc_issue_scgs')
    #     cursor = conn.cursor()
    #     sql = "select * from etc_user_info"
    #     row_count = cursor.execute(sql)
    #     print(row_count)
    #     print(cursor.fetchone())
    #     # 取出结果中所有数据
    #     for line in cursor.fetchall():
    #         print(line)
    #     cursor.close()
    #     conn.close()
    #     return [{"id": self.id, "name": "红楼梦", "code": "007", "image_1920": False,
    #              "__last_update": "2020-07-16 06:11:44", "shelf_id": False, "tags_ids": [],
    #              "display_name": "\u5929\u9f99\u516b\u90e8(False)"}]




class VehicleInfo(models.Model):
    _name = 'etc.vehicle.info'
    _description = 'ETC Vehicle Info'
    _rec_name = 'plateNo'

    plateNo = fields.Char(string='车牌号码')
    plateColor = fields.Selection(
        [("0", "蓝色"), ("1", "黄色"), ("2", "黑色"), ("3", "白色"), ("4", "渐变绿色"), ("5", "黄绿双拼色"), ("6", "蓝白渐变色"),
         ("7", "临时牌照"), ("9", "未确定"), ("11", "绿色"), ("12", "红色")], string='车牌颜色')
    vehClass = fields.Selection([("0", "客车"), ("1", "货车")], string='客货类型')
    vehType = fields.Selection(
        [("1", "一型客车"), ("2", "二型客车"), ("3", "三型客车"), ("4", "四型客车"), ("11", "一型货车"), ("12", "二型货车"), ("13", "三型货车"),
         ("14", "四型货车"), ("15", "五型货车"), ("16", "六型货车"), ("21", "一型专项作业车"), ("22", "二型专项作业车"), ("23", "三型专项作业车"),
         ("24", "四型专项作业车"), ("25", "五型专项作业车"), ("26", "六型专业作业车")], string='车型')
    vehicleColor = fields.Char(string='车辆外观颜色')
    vehicleModel = fields.Char(string='车辆品牌型号')
    engineNo = fields.Char(string='发动机号')
    vin = fields.Char(string='车辆识别代号')
    approvedCount = fields.Integer(string='核载人数')
    vehicleLength = fields.Integer(string='车长')
    vehicleWidth = fields.Integer(string='车宽')
    vehicleHeight = fields.Integer(string='车高')

    obuinfo_ids = fields.One2many('etc.obu.info', 'vehicleinfo_id', string='OBU编号')
    cardinfo_ids = fields.One2many('etc.card.info', 'vehicleinfo_id', string='ETC卡号')
    userinfo_id = fields.Many2one('etc.user.info', string='车辆所有人名称')
    centerUserId = fields.Char(related='userinfo_id.centerUserId', string='用户编号')
    ownerName = fields.Char(related='userinfo_id.name', string='车辆所有人名称')
    ownerAddress = fields.Char(related='userinfo_id.address', string='所有人住址')
    vehicleType = fields.Char(string='行驶证车辆类型')
    useCharacter = fields.Selection(
        [("1", "家庭自用"), ("2", "非营业客车"), ("3", "营业客车"), ("4", "非营业货车"), ("5", "营业货车"), ("6", "特种车"), ("7", "挂车"),
         ("8", "应急救援车"), ("9", "ETC发行方发行国际标准集装箱运输单一用途专用OBU，类型为J1"), ("10", "ETC发行方发行国际标准集装箱运输多用途专用OBU，类型为J2")],
        string='车辆使用性质')
    registerDate = fields.Date(string='注册日期')
    issueDate = fields.Date(string='发证日期')
    fileNum = fields.Char(string='档案编号')
    verifyRecord = fields.Char(string='校验记录')
    totalMass = fields.Integer(string='总质量(KG)')
    maintenanceMass = fields.Integer(string='整备质量(KG)')
    permittedWeight = fields.Integer(string='核定载质量(KG)')
    permittedTowWeight = fields.Integer(string='准牵引总质量(KG)')
    wheelCount = fields.Integer(string='轮数')
    axleCount = fields.Integer(string='车轴数')
    axleDistance = fields.Integer(string='轴距(mm)')
    licensePic1 = fields.Char(string='行驶证图片(主)')
    licensePic2 = fields.Char(string='行驶证图片(副)')


class ObuInfo(models.Model):
    _name = 'etc.obu.info'
    _description = 'ETC Obu Info'
    _rec_name = 'obuId'

    vehicleinfo_id = fields.Many2one('etc.vehicle.info', string='车牌号码')
    cardinfo_ids = fields.One2many('etc.card.info', 'obuinfo_id', string='ETC卡号')
    centerUserId = fields.Char(related='vehicleinfo_id.centerUserId', string='用户编号')
    obuId = fields.Char(string='OBU编号')
    obuMac = fields.Char(string='OBU物理编号')
    plateNo = fields.Char(related='vehicleinfo_id.plateNo', string='车牌号码')
    plateColor = fields.Selection(related='vehicleinfo_id.plateColor', string='车牌颜色')
    issuerFlag = fields.Char(related='cardinfo_ids.issuerFlag', string='发卡方标识')
    obuVer = fields.Char(help='版本号十六进制值，假如为0x42版本就写“42”')
    startTime = fields.Datetime(string='启用时间')
    endTime = fields.Datetime(string='到期时间')
    userType = fields.Selection(
        [("00", "普通用户"), ("18", "对于道路运输证经营范围仅有‘货物专用运输（集装箱）’的车辆"),
         ("1C", "对于道路运输证经营范围除 ‘货物专用运输（集装箱）’外，还有‘普通货运’的车辆"),
         ("1A", "应急救援车"), ("1B", "其他牵引车")], string='车辆用户类型', help='车辆用户类型,见5.2.2')
    obuStatus = fields.Selection([("0", "未发行"), ("1", "一发完毕"), ("2", "二发完毕（正常）"), ("3", "注销"), ("4", "挂失")],
                                 string='obu状态')
    vehType = fields.Selection(related='vehicleinfo_id.vehType', string='车型')
    axleCount = fields.Integer(related='vehicleinfo_id.axleCount', string='轴数')
    wheelCount = fields.Integer(related='vehicleinfo_id.wheelCount', string='轮数')
    weightOrSeat = fields.Integer(string='车辆载重/座位数')


class CardInfo(models.Model):
    _name = 'etc.card.info'
    _description = 'ETC Card Info'
    _rec_name = 'cardId'

    obuinfo_id = fields.Many2one('etc.obu.info', string='Obu')
    vehicleinfo_id = fields.Many2one('etc.vehicle.info', string='车牌号码')
    userinfo_id = fields.Many2one('etc.user.info', string='用户姓名')
    centerUserId = fields.Char(related='userinfo_id.centerUserId', string='用户编号')
    cardNet = fields.Char(string='卡网络编号')
    cardId = fields.Char(string='ETC卡号')
    plateNo = fields.Char(related='vehicleinfo_id.plateNo', string='车牌号码')
    plateColor = fields.Selection(related='vehicleinfo_id.plateColor', string='车牌颜色')
    issuerFlag = fields.Char(string='发卡方标识')
    cardType = fields.Selection([("23", "记账卡"), ("22", "储值卡")], string='ETC卡类型')
    cardVer = fields.Char(help='版本号十六进制值，假如为0x42版本就写“42”')
    startTime = fields.Datetime(string='卡片启用时间')
    endTime = fields.Datetime(string='卡片到期时间')
    cardStatus = fields.Selection([("0", "未发行"), ("1", "一发完毕"), ("2", "二发完毕（正常）"), ("3", "注销"), ("4", "挂失")],
                                  string='卡状态')

    userType = fields.Selection(related='obuinfo_id.userType', string='车辆用户类型')
    vehType = fields.Selection(related='vehicleinfo_id.vehType', string='车型')
    userName = fields.Char(related='userinfo_id.name', string='持卡人姓名')
    userIdType = fields.Selection(related='userinfo_id.idType', string='持卡人证件类型')
    userIdNum = fields.Char(related='userinfo_id.idNo', string='持卡人证件号码')
    phone = fields.Char(related='userinfo_id.phone', string='预留手机号码')
    signingBankId = fields.Char(string='签约银行的ID')
